
package com.atom.office.system.beans;

import com.atom.office.system.ITimerListener;

import java.util.Timer;
import java.util.TimerTask;

/**
 * 定时器

 */
public class ATimer {
    /**
     * @param delay
     * @param listener 时间监听器
     */
    public ATimer(int delay, ITimerListener listener) {
        super();
        this.delay = delay;
        this.listener = listener;
    }

    /**
     * Starts the <code>Timer</code>,
     * causing it to start sending action events
     * to its listeners.
     *
     * @see #stop
     */
    public void start() {
        if (isRunning) {
            return;
        }
        timer = new Timer();
        timer.schedule(new ATimerTask(), delay);
        isRunning = true;
    }


    /**
     * Returns <code>true</code> if the <code>Timer</code> is running.
     *
     * @see #start
     */
    public boolean isRunning() {
        return this.isRunning;
    }

    /**
     * Stops the <code>Timer</code>,
     * causing it to stop sending action events
     * to its listeners.
     *
     * @see #start
     */
    public void stop() {
        if (isRunning) {
            timer.cancel();
            timer.purge();
            isRunning = false;
        }
    }


    /**
     * Restarts the <code>Timer</code>,
     * canceling any pending firings and causing
     * it to fire with its initial delay.
     */
    public void restart() {
        stop();
        start();
    }

    class ATimerTask extends TimerTask {
        public ATimerTask() {
        }

        public void run() {
            try {
                timer.schedule(new ATimerTask(), delay);
                listener.actionPerformed();
            } catch (Exception e) {
            }
        }

    }

    /**
     *
     */
    public void dispose() {
        if (isRunning) {
            timer.cancel();
            timer.purge();
            isRunning = false;
        }
        timer = null;
        listener = null;
    }

    // 定时器是否处于运行状态
    private boolean isRunning;
    // 间隔
    private int delay;
    // 
    private Timer timer;
    //
    private ITimerListener listener;
}
